// components/user-info/user-info.js
const config = require("../../http/env");
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: {
      type: Object,
      value: {},
    },

    showWechat: {
      type: Boolean,
      value: true,
    },
  },

  // lifetimes: {
  //   attached() {
  //     this.getUserInfoByApi();
  //   },
  // },

  /**
   * 组件的初始数据
   */
  data: {
    baseUrl: config.baseUrl,
    imgUrl: ''
    // userInfo: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取用户信息
    // getUserInfoByApi() {
    //   var that = this;
    //   var user = wx.getStorageSync("userInfo");
    //   httpApi.Post("/index/getUser", { user_id: user.id }).then((res) => {
    //     wx.setStorageSync("userInfo", res.data);
    //     that.setData({
    //       userInfo: res.data,
    //     });
    //   });
    // },

    // 打开积分记录
    openRecord() {
      this.triggerEvent("openRecord", true);
    },
    // 打开头像昵称
    openNick() {
      this.triggerEvent("openNick", true);
    },
  },

  observers: {
    'userInfo': function(val) {
      if (val && val.headimg) {
        this.setData({
          imgUrl: val.headimg.includes('http') ? val.headimg : config.baseUrl + val.headimg,
        });
      }
    }
  },

  pageLifetimes: {
    show: function () {
      if (!this.properties.userInfo?.headimg) return
      this.setData({
        imgUrl: this.properties.userInfo.headimg.includes('http') ? this.properties.userInfo.headimg : config.baseUrl + this.properties.userInfo.headimg,
      });
    },
  }
});
